package com.ihrm.company.controller;


import com.ihrm.common.controller.BaseController;
import com.ihrm.common.entity.Result;
import com.ihrm.company.service.CompanyService;
import com.ihrm.company.service.DepartmentService;
import com.ihrm.domain.company.Department;
import com.ihrm.domain.company.DeptListResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/company")
public class DepartmentController extends BaseController {

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private CompanyService companyService;


//    添加部门
    @PostMapping("/department")
    public Result addDept(@RequestBody Department department) {
        department.setCompanyId(parseCompanyId());
        Integer num = departmentService.addDept(department);
        if(num > 0) {
            return Result.SUCCESS();
        }else {
            return Result.FAIL();
        }

    }

//    修改部门
    @PutMapping("/department/{id}")
    public Result updateDept(@PathVariable String id, @RequestBody Department department) {
        department.setId(id);
        Integer num = departmentService.update(department);
        if(num > 0) {
            return Result.SUCCESS();
        }else {
            return Result.FAIL();
        }
    }

    //    根据id查询部门
    @GetMapping("/department/{id}")
    public Result selectDeptById(@PathVariable String id) {
        Department department = departmentService.select(id);
        return Result.SUCCESS(department);
    }

    //    根据id删除部门
    @DeleteMapping("/department/{id}")
    public Result delDeptById(@PathVariable String id) {
        Integer num = departmentService.delete(id);
        if(num > 0) {
            return Result.SUCCESS();
        }else {
            return Result.FAIL();
        }
    }

    @GetMapping("/department")
    public Result findAllDept() {
        String CompanyId = parseCompanyId();
        DeptListResult deptListResult = departmentService.findAll(CompanyId);
        return Result.SUCCESS(deptListResult);
    }

    @GetMapping("/department/condition")
    public Department findByCondition(String code) {
        Department dept = departmentService.findByCondition(code);
        return dept;
    }
}
